/**
 * 
 */
package com.pagepub.expo.action.tag;

import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.pagepub.core.utils.CalendarUtil;
import com.pagepub.core.utils.ExtendString;
import com.pagepub.core.utils.Md5;
import com.pagepub.expo.entity.CorpPublication;

/**
 * @author 郭光达
 * 
 * 2008-5-21
 */
public class ShowBookTag extends TagSupport {

	private static final long serialVersionUID = 733448870963424716L;
	
	private String ctxurl;
	
	private String imgurl;
	
	private String adid;

	private CorpPublication publication;

	StringBuffer str = new StringBuffer();

	@Override
	public int doStartTag() throws JspException {
		JspWriter out = (JspWriter) this.pageContext.getOut();
		try {
			
//			System.out.print(this.getCtxurl());
			CorpPublication publication=(CorpPublication)this.pageContext.getRequest().getAttribute("publication");
			if(publication!=null)
			{	
				out.print(ShowBookString(this.getPublication()));
//				System.out.print(str);
				
			}else{
				String msg="没有设置分页字串变量,getRequest().getAttribute("+publication+")=null!";
				throw new JspException(msg);
			}

		} catch (Exception e) {
			throw new JspTagException(e.getMessage());
		} finally {
			str.delete(0, str.capacity());
		}
		return 1;
	}

	public String ShowBookString(CorpPublication publication) {
		StringBuffer string = new StringBuffer();
		String pwdString ="";
		String startpage ="";
		Date strdate=new Date();
		string.append("<script language=\"javascript\">");
		if (publication.getPubType() == 3) {
			string
					.append("var so = new SWFObject('"+this.getCtxurl()+"/qzreader/singlepage.swf?configxml="+this.getImgurl()+publication.getPubPath()+"', 'index', '100%', '100%', '8', '', true);");
			string.append("	so.addParam('allowFullScreen', 'true') ;");
//			string.append("so.addParam('wmode', 'transparent'");
//			string.append("so.addParam('salign', 't'");
			string
					.append("so.setAttribute('xiRedirectUrl', window.location);");
			string
			.append("so.write('flashcontent'); ");
//			string
//			.append("var so = new SWFObject('"+this.getCtxurl()+"/qzreader/singlepage.swf', 'index', '100%', '100%', '8', '', true);");
//			string
//			.append("so.addParam('FlashVars', 'configxml="+this.getCtxurl()+"/"+publication.getPubpath()+"');");
//			string.append("so.addParam('allowFullScreen', 'true'); ");
//			string
//			.append("so.setAttribute('xiRedirectUrl', window.location);");
//			string
//			.append("so.write('flashcontent'); ");
		} else{
			if(!"".equals(ExtendString.nullToSpaceNoTrim(publication.getPassword()))){
				int daynum=CalendarUtil.diff(strdate,publication.getPwdLapseDate() );
				if(daynum<=1){
					pwdString=new Md5(publication.getPassword()).compute();	
				}					
				
			}
			if(publication.getPwdStartPage()!=0){
				startpage=publication.getPwdStartPage()+"";
			}
			if("".equals(pwdString)){
				startpage="";
			}
			string
					.append("var so = new SWFObject('"+this.getCtxurl()+"/qzreader/ipageBook.swf', 'index', '100%', '100%', '8', '', true);");
			string
					.append("so.addParam('FlashVars', 'configxml="+this.getImgurl()+publication.getPubPath()+"/&passWordAt="+startpage+"&passWord="+pwdString+"&id="+publication.getId()+"&memberid="+publication.getCorpId()+"&adid="+this.getAdid()+"');");
//			System.out.println(this.getImgurl()+publication.getPubpath()+"/&passWordAt="+startpage+"&passWord="+pwdString+"&id="+publication.getPublicationid()+"&memberid="+publication.getMemberid());
			string.append("so.addParam('allowFullScreen', 'true'); ");
//			string.append("so.addParam('align', 'middle'); ");
//			string.append("so.addParam('salign', 't'); ");		
			string
					.append("so.setAttribute('xiRedirectUrl', window.location);");
			string
			.append("so.write('flashcontent'); ");
		}
		string.append("</script>");
		return string.toString();
	}


	
	public CorpPublication getPublication() {
		return publication;
	}

	public void setPublication(CorpPublication publication) {
		this.publication = publication;
	}

	public String getCtxurl() {
		return ctxurl;
	}

	public void setCtxurl(String ctxurl) {
		this.ctxurl = ctxurl;
	}

	public String getImgurl() {
		return imgurl;
	}

	public void setImgurl(String imgurl) {
		this.imgurl = imgurl;
	}

	public String getAdid() {
		return adid;
	}

	public void setAdid(String adid) {
		this.adid = adid;
	}

}
